import {Actions} from "react-native-router-flux";
import fetch from 'dva/fetch'
import apiPath from '../../utils/apiPath'


//长按短按
var timeCount = null;
let pressTime = 0;//按钮按下后计数

export default {
  namespace: 'photo',

  state: {
    images: [],
    signInImgs: [],
    singOutImgs: [],
    SGImgs: [],
    JSImgs: [],
    JLImgs: [],
    allImgs:[],
    imgUrl:'',
    thisOpen:false,
    thisAlertOpen:false,
    deleteImg:'',
    deleteType:''
  },

  subscriptions: {},

  effects: {
    *upload({payload}, {put, call, select}){
      const data = new FormData()

      // Actions.push('Photograph')
      data.append('file', {
        uri: payload.uri,
        type: 'image/jpeg',
        name: payload.name,
      });


        // console.log('=======URI==,',payload.uri)
      const url = yield fetch(GLOBAL.INT.imageUpload, {
        method: 'post',
        body: data
      }).then(res => {
        let data = JSON.parse(res._bodyInit).data
        return data.fileurl
      }).catch(err => {
        console.log(err)
        return null
      })
        // console.log('=======imgUrl==,',url)
      if (url !== null) {
        yield put({type: 'saveSuccess', payload: {url: url, type: payload.type}})
        Actions.pop();

      }
    },

    //按下按钮开始计数
    *thisPressIn({payload},{put,call,select}){
      //开始计数，100ms计数一次
      timeCount = setInterval(
        ()=>{
          pressTime += 1;
        },
        100,
      );
    },
    //松开按钮获取按下后的计数值
    *thisPressOut({payload},{put,call,select}){
      timeCount && clearInterval(timeCount);//停止计数
      if(pressTime > 8){
        // 长按状态下的事件
        yield put({type:'cleanSignThis',payload:payload})

      }else {
        yield put({type:'thisPhotoOpen',payload:payload.url})
      }
      pressTime = 0;
    },

  },

  reducers: {
    cleanSignThis(state,{payload}){
      return {
        ...state,
        deleteImg:payload.url,
        deleteType:payload.photoType,
        thisAlertOpen:true
      }
    },
    thisAlertClose(state,{payload}){
      return {
        ...state,
        thisAlertOpen:false
      }
    },
    saveSuccess(state, {payload}){
      let photoType = payload.type
      if (photoType == 'signInImgs') {
        state['signInImgs'] = state['signInImgs'].concat([{url: payload.url}])
      } else if (photoType == 'signOutImgs') {
        state['singOutImgs'] = state['singOutImgs'].concat([{url: payload.url}])
      } else if (photoType == 'SGImgs') {
        state['SGImgs'] = state['SGImgs'].concat([{url: payload.url}])
      } else if (photoType == 'JSImgs') {
        state['JSImgs'] = state['JSImgs'].concat([{url: payload.url}])
      } else if (photoType == 'JLImgs') {
        state['JLImgs'] = state['JLImgs'].concat([{url: payload.url}])
      } else if (photoType == 'allImgs') {
        state['allImgs'] = state['allImgs'].concat([{url: payload.url}])
      }
      console.log(state['allImgs'])
      return {...state}
    },

    //删除照片
    cleanImages(state,{payload}){
      let imgs = []
      let signInImgs = []
      let singOutImgs = []
      let SGImgs = []
      let JSImgs = []
      let JLImgs = []
      let allImgs = []

      switch (payload.photoType){
        case 'signInImgs':
          imgs = state.signInImgs
          signInImgs = state.signInImgs
          break
        case 'signOutImgs':
          imgs = state.singOutImgs
          singOutImgs = state.singOutImgs
          break
        case 'SGImgs':
          imgs = state.SGImgs
          SGImgs = state.SGImgs
          break
        case 'JSImgs':
          imgs = state.JSImgs
          JSImgs = state.JSImgs
          break
        case 'JLImgs':
          imgs = state.JLImgs
          JLImgs = state.JLImgs
          break
        case 'allImgs':
          imgs = state.allImgs
          allImgs = state.allImgs
          break
        default:break

      }

      // if (payload.photoType === 'signInImgs') {
      //   imgs = state.signInImgs
      //   signInImgs = state.signInImgs
      // } else if (payload.photoType === 'signOutImgs') {
      //   imgs = state.singOutImgs
      //   singOutImgs = state.singOutImgs
      // } else if (payload.photoType === 'SGImgs') {
      //   imgs = state.SGImgs
      //   SGImgs = state.SGImgs
      // } else if (payload.photoType === 'JSImgs') {
      //   imgs = state.JSImgs
      //   JSImgs = state.JSImgs
      // } else if (payload.photoType === 'JLImgs') {
      //   imgs = state.JLImgs
      //   JLImgs = state.JLImgs
      // } else if (payload.photoType === 'allImgs') {
      //   imgs = state.allImgs
      //   allImgs = state.allImgs
      // }

      if(imgs != undefined && imgs != null){
        for(let i in imgs){
          if(payload.url === imgs[i].url){
            imgs.splice(i,1);
          }
        }
      }


      switch (payload.photoType){
        case 'signInImgs':
          signInImgs = imgs
          break
        case 'signOutImgs':
          singOutImgs = imgs
          break
        case 'SGImgs':
          SGImgs = imgs
          break
        case 'JSImgs':
          JSImgs = imgs
          break
        case 'JLImgs':
          JLImgs = imgs
          break
        case 'allImgs':
          allImgs = imgs
          break
        default:break
      }

      // if (payload.photoType === 'signInImgs') {
      //   signInImgs = imgs
      // } else if (payload.photoType === 'signOutImgs') {
      //   singOutImgs = imgs
      // } else if (payload.photoType === 'SGImgs') {
      //   SGImgs = imgs
      // } else if (payload.photoType === 'JSImgs') {
      //   JSImgs = imgs
      // } else if (payload.photoType === 'JLImgs') {
      //   JLImgs = imgs
      // } else if (payload.photoType === 'allImgs') {
      //   allImgs = imgs
      // }

      return {...state,
        signInImgs:[].concat(signInImgs),
        singOutImgs:[].concat(singOutImgs),
        SGImgs:[].concat(SGImgs),
        JSImgs:[].concat(JSImgs),
        JLImgs:[].concat(JLImgs),
        allImgs:[].concat(allImgs),
        thisAlertOpen:false
      }
    },

    //清空照片图片
    cleanPhotoImgs(state, {payload}){
      return {
        ...state,
        images: [],
        signInImgs: [],
        singOutImgs: [],
        SGImgs: [],
        JSImgs: [],
        JLImgs: [],
        allImgs:[],

      }
    },
    //查看大图
    thisPhotoOpen(state,{payload}){
      console.log("model====",payload)
      return{...state,
        imgUrl:payload,
        thisOpen:true
      }

    },
    //关闭查看大图
    thisPhotoClose(state,{payload}){
      return{...state,
        imgUrl:'',
        thisOpen:false
      }
    }
  },

}